﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RentHouse.Common;
using RentHouse.Service;
using RentHouse.DTO;
using RentHouse.IService;
using RentHouse.Service.Entities;

namespace RentHouse.Service
{
   public class PermissionService: IPermissionService
    {   
        /// <summary>
        /// 添加权限
        /// </summary>
        /// <param name="dTO"></param>
        /// <returns></returns>
        public AjaxResult AddPermission(PermissionDTO dTO)
        {
            using (var db=new RentHouseContext())
            {
                BaseService <T_Permissions> service = new BaseService<T_Permissions>(db);
                T_Permissions t_Permissions = new T_Permissions {
                    CreateDateTime=DateTime.Now,
                    Description=dTO.Description,
                    Name=dTO.Name,
                    IsDeleted=false
                };
                if (service.Add(t_Permissions) > 0)
                {
                    return new AjaxResult(ResultState.Success, "添加成功");
                }
                else
                {
                    return new AjaxResult(ResultState.Error, "添加失败");

                }
            }
        }

        /// <summary>
        /// 删除权限
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public AjaxResult DeletePermission(int id)
        {
            using (var db = new RentHouseContext())
            {
                BaseService<T_Permissions> service = new BaseService<T_Permissions>(db);
                var result = service.Get(e => e.Id == id);
                if (result!=null)
                {
                    if (service.Delete(result))
                    {
                        return new AjaxResult(ResultState.Success, "删除成功");
                    }
                    else
                    {
                        return new AjaxResult(ResultState.Success, "删除失败");

                    }
                }
                return new AjaxResult(ResultState.Success, "删除失败");

            }
        }

        /// <summary>
        /// 多选删除权限
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public AjaxResult DeletePers(int[] id)
        {
            using (var db = new RentHouseContext())
            {
                BaseService<T_Permissions> service = new BaseService<T_Permissions>(db);
                try
                {
                    for (int i = 0; i < id.Length; i++)
                    {
                        DeletePermission(id[i]);
                    }
                    return new AjaxResult(ResultState.Success, "删除成功");

                }
                catch (Exception)
                {
                    return new AjaxResult(ResultState.Success, "删除失败");

                }

            }
        }

        /// <summary>
        /// 权限列表的展示
        /// </summary>
        /// <param name="length"></param>
        /// <param name="start"></param>
        /// <param name="name"></param>
        /// <param name="rowCount"></param>
        /// <returns></returns>
        public List<PermissionDTO> GetList(int length, int start,string name,ref int rowCount)
        {
            using (var model = new RentHouseContext())
            {
                var db = new BaseService<T_Permissions>(model);
                var query = PredicateExtensions.True<T_Permissions>();
                if (!string.IsNullOrWhiteSpace(name))
                {
                    query = query.And(e => e.Name.Contains(name));
                }
                var list = db.GetPagedList(start,length,ref rowCount, query, e=>e.Id,false).Select(e => new PermissionDTO
                {
                    Id= e.Id,
                    Name= e.Name,
                    Description= e.Description

                }).ToList();

                
                return list;
            
            }
        }


        /// <summary>
        /// 获取该权限
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public PermissionDTO GetPermission(int id)
        {
            using (var db = new RentHouseContext())
            {
                BaseService<T_Permissions> service = new BaseService<T_Permissions>(db);
                var result = service.Get(e => e.Id == id);
                if (result != null)
                {
                    PermissionDTO permission = new PermissionDTO
                    {
                        Description = result.Description,
                        Name = result.Name,
                        Id = result.Id
                    };
                    return permission;
                }
                return null;


            }
        }

        /// <summary>
        /// 修改权限
        /// </summary>
        /// <param name="dTO"></param>
        /// <returns></returns>
        public AjaxResult UpdatePermission(PermissionDTO dTO) 
        {
            using (var db = new RentHouseContext())
            {
                BaseService<T_Permissions> service = new BaseService<T_Permissions>(db);
                try
                {
                    T_Permissions permission = new T_Permissions
                    {
                        Description = dTO.Description,
                        Name = dTO.Name,
                        Id = dTO.Id,
                        CreateDateTime = DateTime.Now,
                        //IsDeleted = false
                    };

                    if ( service.Update(permission))
                    {
                        return new AjaxResult(ResultState.Success, "修改成功");
                    }
                    else
                    {
                        return new AjaxResult(ResultState.Error, "修改失败");

                    }
                }
                catch (Exception)
                {
                    return new AjaxResult(ResultState.Error, "修改失败");

                }
            }
        }

        public async Task<AjaxResult> UpdatePermissionAsync(PermissionDTO dTO)
        {
            using (var db = new RentHouseContext())
            {
                BaseService<T_Permissions> service = new BaseService<T_Permissions>(db);
                try
                {
                    T_Permissions permission = new T_Permissions
                    {
                        Description = dTO.Description,
                        Name = dTO.Name,
                        Id = dTO.Id,
                        CreateDateTime = DateTime.Now,
                        //IsDeleted = false
                    };

                    if (await service.UpdateAsync(permission))
                    {
                        return new AjaxResult(ResultState.Success, "修改成功");
                    }
                    else
                    {
                        return new AjaxResult(ResultState.Error, "修改失败");

                    }
                }
                catch (Exception)
                {
                    return new AjaxResult(ResultState.Error, "修改失败");

                }
            }
        }
    }
}
